﻿using LdtDataEngin.ModuleDetail.RegCommon;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.DynamicData;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.ParagraphAnalysis
{
    /// <summary>
    /// 利用正则来执行段落提取
    /// </summary>
    public class ParagraphReg
    {
        // 上下文
        private DataContext _dataContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataContext"></param>
        public ParagraphReg(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// 正则提取GetParseList
        /// </summary>
        /// <param name="dynamicDataStores"></param>
        /// <returns></returns>
        public void Extract(ConfigStateModel configModel, string modelName)
        {
            if (_dataContext.DataLayerAns.Duanluos == null)
            {
                _dataContext.DataLayerAns.Duanluos = new List<An_duanluo>();
            }
            ProcessAnalysis processAnalysis = new ProcessAnalysis(_dataContext, modelName);
            List<string> valueList = new List<string>();
            //原文
            string originalText = "";
            // 判断当前的段落中是否已经有了机器学习生成的段落，如果有就拷贝机器学习的结果，不再进行正则提取
            List<An_duanluo> duanluos = new List<An_duanluo>();
            duanluos = _dataContext.DataLayerAns.Duanluos.Where(a => a.Duanluobianhao.ToLower() == configModel.code.ToLower()).ToList();
            if (duanluos.Count > 0)
            {
                _dataContext.InteractiveShow.LogShow("\t段落提取:===============" + configModel.code + "段落内容已经使用机器学习提取到，结果是:" + duanluos[0].Duanluoneirong);
            }
            else
            {
                // 机器学习未提出的段落继续使用正则提取
                if (configModel.number.ToString().IndexOf(".") < 0)
                {   //根节点
                    originalText = _dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong;
                    valueList.Add(originalText);
                    InteractiveModel interactiveModel = processAnalysis.AnalysisProcessData(configModel.code, modelName, 1, valueList);
                    if (interactiveModel != null)
                    {
                        foreach (string content in interactiveModel.ModelList)
                        {
                            An_duanluo duanluo = new An_duanluo();
                            duanluo.Duanluoid = Snowflake.Instance().GetId().ToString();
                            duanluo.Duanluoneirong = content.Trim();
                            duanluo.Duanluobianhao = configModel.code.ToLower();
                            duanluo.DuanluoxuhaoDesc = configModel.number;
                            duanluo.Duanluomingcheng = configModel.zh_cn;
                            duanluo.Duanluoyuanwen = originalText;
                            _dataContext.DataLayerAns.Duanluos.Add(duanluo);
                            _dataContext.InteractiveShow.LogShow("段落名称：" + configModel.executecode + " 段落内容：" + content);
                        }
                    }
                } 
                else
                {
                    //子集节点
                    string[] numArry = configModel.number.Split('.');
                    var length = numArry.Length;
                    string parentNo =string.Join(".", numArry[..(length-1)]);
                    List<An_duanluo> an_Duanluos = _dataContext.DataLayerAns.Duanluos.Where(a => a.DuanluoxuhaoDesc == parentNo).ToList();
                    foreach (An_duanluo item in an_Duanluos)
                    {
                        valueList = new List<string>();
                        valueList.Add(item.Duanluoneirong);
                        InteractiveModel interactiveModel = processAnalysis.AnalysisProcessData(configModel.code, modelName, 1, valueList);
                        if (interactiveModel != null)
                        {
                            foreach (string content in interactiveModel.ModelList)
                            {
                                An_duanluo duanluo = new An_duanluo();
                                duanluo.Duanluoid = Snowflake.Instance().GetId().ToString();
                                duanluo.Duanluoneirong = content.Trim();
                                duanluo.Duanluobianhao = configModel.code.ToLower();
                                duanluo.DuanluoxuhaoDesc = configModel.number;
                                duanluo.Duanluofujiid = item.Duanluoid;
                                duanluo.Duanluomingcheng = configModel.zh_cn;
                                duanluo.Duanluoyuanwen = item.Duanluoyuanwen;
                                _dataContext.DataLayerAns.Duanluos.Add(duanluo);
                                _dataContext.InteractiveShow.LogShow("段落名称：" + configModel.executecode + " 段落内容：" + content);
                            }
                            
                        }
                    } 
                }
            }
           
        }

        /// <summary>
        /// 机器学习提取段落，在当前类中无需实现
        /// </summary>
        /// <returns></returns>
        public void AIExtract()
        {
            //return _dataContext.DataLayerAns;
        }

    }
}
